//
//  XMPicwordCommentView.m
//  XMJJ_zmy
//
//  Created by 丁浩 on 2025/7/10.
//

#import "XMPicwordCommentView.h"

@interface XMPicwordCommentView ()<UITextViewDelegate>

@property (nonatomic, strong) UITextView *contentTextView;
@property (nonatomic, strong) UILabel *placeHolderLabel;

@end

@implementation XMPicwordCommentView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMiss)];
    [self addGestureRecognizer:tap];
    
    self.backgroundColor = kColorHEXA(0x464646, 0.46);
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height, self.width, KScaleWidth(182.0))];
    [contentView setCornerRadius:KScaleWidth(10.0)];
    contentView.backgroundColor = kColorHEX(0xffffff);
    [self addSubview:contentView];
    [contentView addSubview:self.contentTextView];
    [_contentTextView addSubview:self.placeHolderLabel];
    UIButton *publishBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:CGRectMake(self.width - KScaleWidth(15.0) - KScaleWidth(74.0), _contentTextView.maxY + KScaleWidth(10.0), KScaleWidth(74.0), KScaleWidth(34.0)) title:@"发表" titleColor:kColorHEX(0x333333)];
    [publishBtn addTarget:self action:@selector(publishAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:publishBtn];
//    self.alpha = 0;
}

- (void)setContentWithName:(NSString *)name {
    if ([name containsString:@"说点"]) {
        _placeHolderLabel.text = name;
    }
    else {
        _placeHolderLabel.text = [NSString stringWithFormat:@"回复 @%@", name];
    }
}

- (void)publishAction:(UIButton *)button {
    self.publishBlock ? self.publishBlock(self.contentTextView.text) : nil;
    [self disMiss];
}

- (void)show {
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.alpha = 1;
        weakSelf.contentTextView.superview.y = weakSelf.height - weakSelf.contentTextView.superview.height;
        [weakSelf.contentTextView becomeFirstResponder];
    }];
}

- (void)disMiss {
    [self endEditing:YES];
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.alpha = 0;
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

- (void)textViewDidChange:(UITextView *)textView {
    _placeHolderLabel.hidden = (textView.text.length > 0);
}

#pragma mark - Init
- (UITextView *)contentTextView {
    if (!_contentTextView) {
        _contentTextView = [[UITextView alloc] initWithFrame:CGRectMake(KScaleWidth(15.0), KScaleWidth(20.0), self.width - KScaleWidth(15.0)*2, KScaleWidth(108.0))];
        _contentTextView.backgroundColor = kColorHEX(0xF7F7F7);
        [_contentTextView setCornerRadius:KScaleWidth(7.0)];
        _contentTextView.font = KFontSize(14.0);
        _contentTextView.contentInset = UIEdgeInsetsMake(KScaleWidth(16.0), KScaleWidth(16.0), KScaleWidth(16.0), KScaleWidth(16.0));
        _contentTextView.delegate = self;
    }
    return _contentTextView;
}

- (UILabel *)placeHolderLabel {
    if (!_placeHolderLabel) {
        _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(3.0), KScaleWidth(6.0), self.contentTextView.width, KScaleWidth(20.0)) font:KFontSize(14.0) textColor:kColorHEX(0x666666)];
        _placeHolderLabel.text = @"说点什么吧！";
        _placeHolderLabel.userInteractionEnabled = NO;
    }
    return _placeHolderLabel;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
